define(function(require){
    $(function(){
    	/*---------- 财务管理——供应商/商铺运营人员 ----------*/
    	
    	/* 表格 */
    	var org_id = semi.utils.getUrlParam('org_id');
    	 window.costToFixed=function(cost){
         	if(/(^[^0]\d*$)/.test(cost)){
         		return cost;
         	}
         	if(cost && cost!=0)
         		return new Number(cost).toFixed(2);
         	return cost;
         };
    	var costHistorybodyTemplate = ['<tr>',
			    	                    '<td class="txtc">{date}</td>',
			    	                    '<td class="txtc">@costToFixed({in_cost})</td>',
			    	                    '<td class="txtc">@costToFixed({out_cost})</td>',
			    	                    '<td class="txtc">@costToFixed({ally_cost})</td>',
				                     '</tr>'].join('');
        var costHistoryGrid = Grid.init({
	            holder:'costHistoryList',
	            bodyTemplate:costHistorybodyTemplate,
	            dataSource:'/trp/cost/get_cost_list',
	            pageTemplate:semi.grid.pageTemplate,
	            sendOnPageSize: true
	        });
        
        var chargeTemplate = ['<tr">',
   	    	                    '<td class="txtc">{time}</td>',
   	    	                    '<td class="txtc">@costToFixed({in_balance})</td>',
   	    	                 	'<td class="txtc">@costToFixed({out_balance})</td>',
   	    	                 	'<td class="txtc">@costToFixed({ally_balance})</td>',
   		                     '</tr>'].join('');
               
        var chargeGridInit = false,
        	chargeGrid = Grid.init({
    			holder:'chargeList',
    			bodyTemplate:chargeTemplate,
    			dataSource:'/trp/cost/get_record_list',
    			pageTemplate:semi.grid.pageTemplate,
    			sendOnPageSize: true
            });
        
        costHistoryGrid.onfilled(function(){
        	if(!chargeGridInit){
        		setTimeout(function(){
        			chargeGrid.send({
        	    		pageSize: 20,
        	    		pageNo: 1
        	    	});
        		},100);
        	}
        });
        
        chargeGrid.onfilled(function(){
        	chargeGridInit = true;
        });
        
        costHistoryGrid.send({
    		pageSize: 20,
    		pageNo: 1
    	});
        
        
        var updateCurrentData = function(data){
        	if(data.in_cost=='-0.00')data.in_cost=0;
        	if(data.out_cost=='-0.00')data.out_cost=0;
        	if(data.ally_cost=='-0.00')data.ally_cost=0;
        	if(data.in_balance=='-0.00')data.in_balance=0;
        	if(data.out_balance=='-0.00')data.out_balance=0;
        	if(data.ally_balance=='-0.00')data.ally_balance=0;
        	$('.in_cost').html(costToFixed(data.in_cost));
        	$('.out_cost').html(costToFixed(data.out_cost));
        	$('.ally_cost').html(costToFixed(data.ally_cost));
        	$('.in_balance').html(costToFixed(data.in_balance));
        	$('.out_balance').html(costToFixed(data.out_balance));
        	$('.ally_balance').html(costToFixed(data.ally_balance));
        };
        
        /* 获取用户余额及花费 */
        $ajax.ajax({
			url : '/trp/cost/get_current_data',
            success: function(data){
            	updateCurrentData(data);
            },
            error: function(){
            	var data = {in_balance:0, out_balance:0, ally_balance:0, in_cost:0, out_cost:0, ally_cost:0};
            	updateCurrentData(data);
            }
    	 });
    });
});